import java.util.ArrayList;
import java.util.Iterator;

import org.w3c.dom.Document;

import com.twitknot.TweetParser;

import info.daveturner.FileUtils;
import info.daveturner.HttpUtils;
import info.daveturner.XMLUtils;


public class Runner {

  public static void main(String[] args) {
    
    String xml="";
    Document dom=null;
    ArrayList<String> tweets = null;
    ArrayList<String> tweetLinks = null;
    TweetParser tp = null;
    Iterator<String> it;
    
    try{
      xml = FileUtils.readFileAsString("xml/home_timeline.xml");
    }catch(Exception e){
      e.printStackTrace();
      System.out.println("Could not get xml string");
      System.exit(0);
    }
    
    try{
      dom = XMLUtils.getDomParser(xml);
    }catch(Exception e){
      e.printStackTrace();
      System.out.println("Could not get a parser");
      System.exit(0);
      
    }
    if(dom == null){
      System.out.println("Could not get a parser");
      System.exit(0);
    }
    
    
    
    
    try{
      tp  = new TweetParser(dom);
    }catch(Exception e){
      System.out.println("Exception initiliazing tweet parser");
      System.exit(0);
    }
   
    if(tp == null){
      System.out.println("Could not get a parser");
      System.exit(0);
    }
   
    tweets = tp.getTweets();
    /*
    if(tweets != null){
      it = tweets.iterator();
      while(it.hasNext()) {
        System.out.println("Tweet");
        System.out.println(it.next());
      }
    }
    */
    
    
    tweetLinks = tp.getTweetLinks();
    System.out.println("Tweet Links: " + tweetLinks.size());
    if(tweetLinks != null){
      it = tweetLinks.iterator();
      while(it.hasNext()) {
        try{
          String origUrl = it.next();
          System.out.println("Original URL: " + origUrl);
          System.out.println(" Resolved URL: " + HttpUtils.getResolvedUrl( origUrl));
        }catch(Exception e){
          e.printStackTrace();
          System.out.println("error: " + e.getMessage());
        }
        
      }
    }
    /*
    String tweet="";
    //tweet="This is a messedup link http://www.noonehttp://www.thing.com";
    //tweet="mamamayarin: ?????????????????wRT @Bkae3: ???????RT @mamamayarin: ?????????????? http://tweetphoto.com/35903636";
    //tweet ="http://www.google.com";
    //tweet ="http://www.google.com http://www.daveturner.info";
    //tweet=" this is a link http://www.daveturner.info and this is a link https://www.google.com";
    //tweet="I am a tweet with no link but http://www.daveturner.info wait there is a www.google.com link";
    System.out.println("Do we have any links");
    tweetLinks = tp.getLinks(tweet);
    if(tweetLinks != null){
      it = tweetLinks.iterator();
      while(it.hasNext()) {
        System.out.println(it.next());
      }
    }
    */
    
    
    
    

  }

}
